/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.admin;

import sw6.digipecs.R;
import sw6.digipecs.exceptions.IDNotFoundException;
import sw6.digipecs.model.Situation;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EditSituationNameActivity extends Activity implements OnClickListener {
	public static final int REQUEST_CODE = 33;
	public static final String SITUATION_ID = "situation_id"; 
	private Button mButtonOk;
	private EditText mEditText;
	private Button mButtonCanel;
	private Intent mIntent;
	private int mSituationId;
	private Context mContext;
	private Situation mSituation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialogedittextlayout); 
		initialise(); 
	}

	private void initialise() {
		mButtonOk = (Button) findViewById(R.id.editSituationOkButton);
		mButtonOk.setOnClickListener(this); 
		mButtonCanel = (Button)findViewById(R.id.editSituationCancelButton); 
		mButtonCanel.setOnClickListener(this); 
		
		mEditText = (EditText)findViewById(R.id.editTextName); 
		mIntent = getIntent(); 
		mSituationId = mIntent.getExtras().getInt(SITUATION_ID); 
		mContext = getApplicationContext(); 
		try {
			mSituation = new Situation(mContext,mSituationId);
		} catch (IDNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		mEditText.setText(mSituation.getName()); 
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.editSituationCancelButton:
			setResult(RESULT_CANCELED); 
			finish(); 
			break;

		case R.id.editSituationOkButton:
			mSituation.updateNameInDb(mContext, mEditText.getText().toString()); 
			setResult(RESULT_OK); 
			finish(); 
			break;
		}
	}
	@Override
	public void finish() {
		setResult(RESULT_CANCELED); 
		super.finish();
	}
}
